package com.glacier.yuyuan.modules.admin.sys.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.common.web.BaseController;
import com.glacier.yuyuan.modules.admin.sys.domain.Role;
import com.glacier.yuyuan.modules.admin.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * date 2022-11-13 12:35
 * 角色控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("${yuyuan.adminPath}/sys/role")
@Controller
public class RoleController extends BaseController {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("")
    public String list() {
        return "admin/sys/role/roleList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<Role>> page(Page<Role> page, Role role) {
        page = roleService.findPage(page, role);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(Role role, Model model) {
        if (StrUtil.isNotBlank(role.getId())) {
            role = roleService.findById(role.getId());
        }
        model.addAttribute("entity", role);
        return "admin/sys/role/roleForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(Role role) {
        return Result.ok("操作成功！", roleService.save(role));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        roleService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
